library(foreign)
library(expss)
library(cregg)
library(ggplot2)
library(ggpubr)
library(plyr)
library(tm)
library(wordcloud)
library(readxl)
library(SnowballC)

##Plot figures
#Figure 1

cj4 = apply_labels(cj4,
                   source = "Source of Income",
                   income = "Level of Income",
                   tax2 = "% Paid in Sales Taxes")
cj4$income <- revalue(cj4$income, c("Low"="Low Income", "Middle"="Middle Income", "High"="High Income"))

main <- choice ~ income + source + tax2

MM4w <- cj(cj4, main, id = ~id, estimate = "mm", weights=~webal, by = ~country, level_order = "descending")

AMCE4w <- cj(cj4, main, id = ~id, estimate = "amce", weights=~webal, by = ~country, level_order = "descending")

mm_bw_w <- plot(MM4w, group = "country", xlab="Marginal Mean", vline = 0.5, vline_color = "grey") + 
  aes(shape = country) +
  scale_shape_manual(name="Country", values=c(1,2,0,4), na.translate=F) +
  scale_colour_manual(values=c("black", "black", "black", "black"), na.translate=F) +
  ggplot2::theme_grey() +
  ggtitle("Panel A: MMs") +
  guides(color="none") +
  theme(legend.position="bottom")

amce_bw_w <- plot(AMCE4w, group = "country", xlab="Change in Pr(Selected for Higher Tax Rate)", vline = 0.0, vline_color = "grey", xlim=c(0.0,0.5)) + 
  aes(shape = country) +
  scale_shape_manual(name="Country", values=c(1,2,0,4), na.translate=F) +
  scale_colour_manual(values=c("black", "black", "black", "black"), na.translate=F) +
  ggplot2::theme_grey() +
  ggtitle("Panel B: AMCEs") +
  guides(color="none") +
  theme(legend.position="bottom")

ggarrange(mm_bw_w, amce_bw_w, ncol=1, 
          nrow=2, common.legend = TRUE, legend="bottom", 
          font.label = list(size=12, face="bold")
) %>% ggexport(filename = "fig1.pdf")
  
#Figure 4

USdata = apply_labels(USdata,
                      source = "Source of \n Income",
                      income = "Level of \n Income",
                      tax = "% Paid in \n Sales Taxes")
USdata$source <- revalue(USdata$source, c("Social background"="Social \n background", "State benefit"="State \n benefit"))

USby_income_w <- cj(USdata, choice ~ source + tax, id = ~ id, estimate = "mm", weights=~webal, by = ~ income, h0=0.5, level_order = "descending")

pUSg_w <- plot(USby_income_w, group= "income", vline = 0.5, legend_title = "Level of Income in Profile", vline_color = "grey") + 
  theme(axis.title.x=element_blank()) + 
  scale_x_continuous(limits = c(0.1, 0.9)) + 
  ggtitle("US") +
  scale_colour_grey(na.translate=F)


AUSdata = apply_labels(AUSdata,
                       source = "Source of \n Income",
                       level = "Level of \n Income",
                       tax = "% Paid in Sales Taxes")
AUSdata$source <- revalue(AUSdata$source, c("Social background"="Social \n background", "State benefit"="State \n benefit"))

AUSby_income <- cj(AUSdata, choice ~ source + tax, id = ~ id, estimate = "mm", by = ~ income, h0=0.5, level_order = "descending")

pAUSg <- plot(AUSby_income, group= "income", vline = 0.5, legend_pos = "none", vline_color = "grey") + 
  theme(axis.text.y = element_blank()) + 
  theme(axis.title.x=element_blank()) + 
  scale_x_continuous(breaks=c(0.25,0.5,0.75)) + 
  scale_x_continuous(limits = c(0.1, 0.9)) + 
  ggtitle("Australia") +
  scale_colour_grey()


CHdata = apply_labels(CHdata,
                      source = "Source of \n Income",
                      income = "Level of \n Income",
                      tax = "% Paid in \n Sales Taxes")
CHdata$source <- revalue(CHdata$source, c("Social background"="Social \n background", "State benefit"="State \n benefit"))

CHby_income <- cj(CHdata, choice ~ source + tax, id = ~ id, estimate = "mm", by = ~ income, h0=0.5, level_order = "descending")

pCHg <- plot(CHby_income, group= "income", vline = 0.5, legend_pos = "none", vline_color = "grey") + 
  theme(axis.title.x=element_blank()) + 
  scale_x_continuous(limits = c(0.1, 0.9)) +  
  ggtitle("Chile") +
  scale_colour_grey()


ARGdata = apply_labels(ARGdata,
                       source = "Source of \n Income",
                       income = "Level of \n Income",
                       tax = "% Paid in \n Sales Taxes")

ARGdata$source <- revalue(ARGdata$source, c("Social background"="Social \n background", "State benefit"="State \n benefit"))

ARGby_income <- cj(ARGdata, choice ~ source + tax, id = ~ id, estimate = "mm", by = ~ income, h0=0.5, level_order = "descending")

pARGg <- plot(ARGby_income, group= "income", vline = 0.5, legend_pos = "none", vline_color = "grey") + 
  theme(axis.text.y = element_blank()) + 
  ggtitle("Argentina") +
  theme(axis.title.x=element_blank()) + 
  scale_x_continuous(limits = c(0.1, 0.9)) +
  scale_colour_grey()

ggarrange(pUSg_w, pAUSg, pCHg, pARGg, ncol=2, 
          nrow=2, common.legend = TRUE, legend="bottom", 
          widths = c(0.55, 0.45),
          label.y=0.16,
          vjust=1.8,
          font.label = list(size=12, color="red", face="plain")
) %>%
  ggexport(filename = "fig4.pdf")

#Figure 5

mmdiffUS_r_ideo_w <- cj(USdata, choice ~ income + source + tax, id = ~id, estimate = "mm_differences", weights=~webal, by = ~r_ideo,  level_order = "descending")

pdiffUSbw_w <- plot(mmdiffUS_r_ideo_w, xlab = "", legend_pos = "none", xlim=c(-0.12,0.12), vline = 0) + 
  theme(plot.title = element_text(hjust = 0.5, size=12)) + 
  ggtitle("US") +
  scale_colour_manual(values=c("black", "black", "black")) +
  ggplot2::theme_grey() +
  guides(colour="none")

mmdiffAUS_r_ideo <- cj(AUSdata, choice ~ income + source + tax, id = ~id, estimate = "mm_differences", h0=0.5, by = ~r_ideo, level_order = "descending")

pdiffAUSbw <- plot(mmdiffAUS_r_ideo, xlab = "", legend_pos = "none", xlim=c(-0.12,0.12), vline = 0) + 
  ggtitle("Australia") + 
  theme(plot.title = element_text(hjust = 0.5, size=12)) +
  scale_colour_manual(values=c("black", "black", "black")) +
  ggplot2::theme_grey() +
  theme(axis.text.y=element_blank()) +
  guides(colour="none")

mmdiffCH_r_ideo <- cj(CHdata, choice ~ income + source + tax, id = ~id, estimate = "mm_differences", by = ~r_ideo, level_order = "descending")

pdiffCHbw <- plot(mmdiffCH_r_ideo, xlab = "", legend_pos = "none", xlim=c(-0.12,0.12), vline = 0) + 
  ggtitle("Chile") + 
  theme(plot.title = element_text(hjust = 0.5, size=12)) +
  scale_colour_manual(values=c("black", "black", "black")) +
  ggplot2::theme_grey() +
  guides(colour="none")

mmdiffARG_r_ideo <- cj(ARGdata, choice ~ income + source + tax, id = ~id, estimate = "mm_differences", h0=0.5, by = ~r_ideo, level_order = "descending")

pdiffARGbw <- plot(mmdiffARG_r_ideo,xlab = "", legend_pos = "none", xlim=c(-0.12,0.12), vline = 0) + 
  ggtitle("Argentina") +  
  theme(plot.title = element_text(hjust = 0.5, size=12)) +
  scale_colour_manual(values=c("black", "black", "black")) +
  ggplot2::theme_grey() +
  theme(axis.text.y=element_blank()) +
  guides(colour="none")

ggarrange(pdiffUSbw_w, pdiffAUSbw, pdiffCHbw, pdiffARGbw, ncol=2, 
          nrow=2, common.legend = TRUE, legend="none", 
          widths = c(0.55, 0.45),
          label.y=0.16,
          vjust=1.8,
          font.label = list(size=12, color="red", face="plain")
) %>%
  ggexport(filename = "fig5.pdf")


#Figure 2
#Wordcloud for US respondents who chose profiles with state benefit source of income
state <- subset(justUS, ch_state==1)
text_st <- state$just

corp3 <- Corpus(VectorSource(text_st))
corp3 <- tm_map(corp3, removeNumbers)
corp3 <- tm_map(corp3, removePunctuation)
corp3 <- tm_map(corp3, stripWhitespace)
corp3 <- tm_map(corp3, content_transformer(tolower))
corp3 <- tm_map(corp3, removeWords, stopwords("english"))

dtm3 <- TermDocumentMatrix(corp3) 
matrix3 <- as.matrix(dtm3) 
words3 <- sort(rowSums(matrix3),decreasing=TRUE) 
df3 <- data.frame(word = names(words3),freq=words3)

set.seed(5)
wordcloud(words = df3$word, freq = df3$freq, scale=c(2, 0.5),
          max.words=70, random.order=FALSE, rot.per=0.35)


#Figure 3
#Wordcloud for US respondents who chose profiles with lower share paid in sales taxes
tax_low <- subset(justUS, ch_lowertax==1)
text_tl <- tax_low$just

corp4 <- Corpus(VectorSource(text_tl))
corp4 <- tm_map(corp4, removeNumbers)
corp4 <- tm_map(corp4, removePunctuation)
corp4 <- tm_map(corp4, stripWhitespace)
corp4 <- tm_map(corp4, content_transformer(tolower))
corp4 <- tm_map(corp4, removeWords, stopwords("english"))

dtm4 <- TermDocumentMatrix(corp4) 
matrix4 <- as.matrix(dtm4) 
words4 <- sort(rowSums(matrix4),decreasing=TRUE) 
df4 <- data.frame(word = names(words4),freq=words4)

set.seed(2)
wordcloud(words = df4$word, freq = df4$freq, scale=c(2, 0.5),
          max.words=70, random.order=FALSE, rot.per=0.35)



